//
//  CyTextFieldValid.m
//  QooccNews
//
//  Created by yangxi on 14-3-14.
//  Copyright (c) 2014年 王顺强. All rights reserved.
//

#import "CyTextFieldValid.h"

@implementation CyTextFieldValid

+ (CyTextFieldValid *)tfdValid:(UITextField *)tfd type:(kValidType)type tip:(NSString *)tip
{
    CyTextFieldValid *tfdValid = [[CyTextFieldValid alloc] init];
    
    tfdValid.tfd = tfd;
    tfdValid.type = type;
    tfdValid.tip = tip;
    
    return tfdValid;
}

+ (CyTextFieldValid *)tfdValid:(UITextField *)tfd tip:(NSString *)tip equalValue:(NSString*)value
{
    CyTextFieldValid *tfdValid = [CyTextFieldValid tfdValid:tfd type:kValidTypeEqual tip:tip];
    tfdValid.equalValue = value;
    return tfdValid;
}

+ (CyTextFieldValid *)tfdValid:(UITextField *)tfd tip:(NSString *)tip min:(NSInteger)minLength max:(NSInteger)maxLength
{
    CyTextFieldValid *tfdValid = [CyTextFieldValid tfdValid:tfd type:kValidTypeLength tip:tip];
    tfdValid.minLength = minLength;
    tfdValid.maxLength = maxLength;
    return tfdValid;
}

+ (CyTextFieldValid *)tfdValid:(UITextField *)tfd tip:(NSString *)tip regularValue:(NSString*)value
{
    CyTextFieldValid *tfdValid = [CyTextFieldValid tfdValid:tfd type:kValidTypeRegular tip:tip];
    tfdValid.equalValue = value;
    return tfdValid;
}


- (BOOL)isValidTrue
{
    BOOL result = NO;
    if (_type == kValidTypeEmpty) {
        result = ![_tfd isEmpty];
    } else if (_type == kValidTypePhone) {
        result = [_tfd isValidPhone];
    } else if (_type == kValidTypeEmail) {
        result = [_tfd isValidEmail];
    } else if (_type == kValidTypeEqual) {
        result = [_tfd.text isEqualToString:_equalValue];
    } else if (_type == kValidTypeLength) {
        result = [_tfd isCountInMin:_minLength andMax:_maxLength];
    } else if (_type == kValidTypeRegular) {
        result = [_tfd isMatchedRegularStr:_equalValue];
    }
    
    return result;
}

@end
